var _table_tpl = "";
var _table_tr_tpl = "";

// 
// var _task_last;
// var _task_initial;
var _track_daily;
var _content_main;
var _task_start;
var _task_name;
var _tracks = [];
// var _ele_last;


_table_tpl += '<table id="track_daily" >';
_table_tpl += '<thead><tr><th>Task</th><th>Start</th><th>End</th><th>Duration</th><th>Description</th><th title="Actions">Act</th></tr></thead>';
_table_tpl += '<tfoot><tr><th colspan="6" id="total_time">Total Hrs : 00:00:00</th></tr></tfoot>';
_table_tpl += '<tbody></tbody>';
_table_tpl += '</table>';


_table_tr_tpl = "<tr><td class='name'>@name@</td><td>@start@</td><td class='end'>@end@</td><td class='duration'>@duration@</td><td>@description@</td><td><a title='Repeat this task' class='repeat'>R</a></td></tr>";


function _track_init(){
  _content_main = jQuery('#content_main');

  _task_start = jQuery('#task_start');
  _task_name = jQuery('#task_name');
  
  _track_daily = jQuery('#track_daily');
  
  _frmTrack = jQuery('#frmTrack');
  _frmTrack.submit(function(e){
    _new_task();
    return false;
  });
  
  
  buildTable();
  updateTable();
 
  if(_tracks.length>0) init_task();
  
  
    _task_start.click(function(e){
        _new_task();
    });
}

function _new_task(){
    init_task(true);
    var _ele = addTrack(_task_name.val(),new Date(),'');
     _ele.action = 'track.create';
     $.post('services.php',_ele,showResponse,'json');
     
    updateTable(); 
}

function buildTable(){    
    if(_track_daily.length>0) return true;
    
    _track_daily = jQuery(_table_tpl);
    _content_main.append(_track_daily) ;
}

function buildTableRow(p_name,p_start,p_end,p_description){
    
    if(p_end!=false){
        var _task_duration = calculateDuration(p_start,p_end);  
    }

    
    var __table_tr_tpl = _table_tr_tpl;
		__table_tr_tpl = __table_tr_tpl.replace("@name@",p_name); 
		__table_tr_tpl = __table_tr_tpl.replace("@start@",dateFormat(p_start, "HH:MM:ss")); 
		__table_tr_tpl = __table_tr_tpl.replace("@end@",p_end!=false?dateFormat(p_end, "HH:MM:ss"):'<img src="images/progress_clock.gif" >'); 
		__table_tr_tpl = __table_tr_tpl.replace("@duration@",p_end!=false?dateFormat(_task_duration, "HH:MM:ss"):'-'); 		
		__table_tr_tpl = __table_tr_tpl.replace("@description@","<input size='0' value='"+p_description+"'>");  
        
    _task_last_row = jQuery(__table_tr_tpl);
    _track_daily.prepend(_task_last_row);
}

function updateTable(){
    jQuery('tbody tr',_track_daily).remove();
    
    if(_tracks.length>1){
        var _init = _tracks[0];
        var _end = _tracks[_tracks.length-1];
        var _task_duration = calculateDuration(_init.start,_end.start);
        jQuery('tfoot th',_track_daily).html('Total Hrs: '+dateFormat(_task_duration, "HH:MM:ss"));    
    }
    
    for(i=0; i<_tracks.length; i++ ){
        var _t = _tracks[i];
        buildTableRow(_t.name,_t.start,_t.end,_t.description);
    }        
}


function init_task(clean){
	$('#clock').remove();
	
	$('#counter').prepend('<div id="clock"></div>');

    var _startTime = '00:00:00';
    
    if(_tracks.length>1 && !clean){
        var _now = new Date();
        var _end = _tracks[_tracks.length-1];

        var _task_duration = calculateDuration(_end.start,_now); 
        _startTime=dateFormat(_task_duration, "HH:MM:ss");    
    }
        

	$('#clock').flipify({
		startTime: _startTime,
		image:'images/digits.png'
	});
}


function calculateDuration(p_start, p_end){
    var _duration = new Date();
    _duration.setHours(0,0,0,0);
    _duration.setTime(_duration.getTime() + p_end.getTime() - p_start.getTime());
    
    return _duration;
}


function addTrack(p_name,p_start,p_description){
    if(!p_start) p_start = new Date();
    
    if(_tracks.length>0){
        var _last_track = _tracks[_tracks.length-1];
        if(_last_track)_last_track.end = p_start;    
    }

    
    var _ele = {
        'name':p_name,
        'start': p_start,
        'end': false,
        'description': p_description
        };
    _tracks.push(_ele); 
    
    return _ele;
}



jQuery(document).ready(function() {
	var _repeat = jQuery('.repeat');
	_repeat.live('click', function() {
		var _html = jQuery(this).parent().parent().find('.name').html();
		jQuery('#task_name').val(_html);
	});
});